/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean;

import com.aptech.entities.TblManager;
import com.aptech.entities.TblUser;
import com.aptech.mBean.util.JsfUtil;
import com.aptech.sBean.TblManagerFacade;
import com.aptech.sBean.TblUserFacade;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;

/**
 *
 * @author LanhAnh
 */
@ManagedBean(name = "loginController")
@SessionScoped
public class LoginController {

    @EJB
    private TblUserFacade tblUserFacade;
    @EJB
    private TblManagerFacade tblManagerFacade;

    public TblUserFacade getTblUserFacade() {
        return tblUserFacade;
    }

    public TblManagerFacade getTblManagerFacade() {
        return tblManagerFacade;
    }
    public static final String MANAGER_SESSION_KEY = "ManagerSession";
    public static final String User_SESSION_KEY = "UserSession";
    private String username;
    private String password;
    private TblUser userLogin;
    private TblManager managerLogin;

    public TblUser getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(TblUser userLogin) {
        this.userLogin = userLogin;
    }

    public TblManager getManagerLogin() {
        return managerLogin;
    }

    public void setManagerLogin(TblManager managerLogin) {
        this.managerLogin = managerLogin;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String userRegister() {
        return "faces/Admin/User/Create.xhtml";
    }

    public void checkUserLogin(javax.faces.event.ActionEvent actionEvent) {
        try {
            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg = null;
            boolean loggedIn = false;
            List<TblUser> list = tblUserFacade.checkLogin(username, password);
            username = "";
            password = "";
            if (list.size() > 0) {
                userLogin = list.get(0);
                JsfUtil.putSession(User_SESSION_KEY, userLogin);
                loggedIn = true;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", userLogin.getUsUserName());
                FacesContext.getCurrentInstance().addMessage(null, msg);
                context.addCallbackParam("loggedIn", loggedIn);
            } else {
                loggedIn = false;
                userLogin = null;
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "User name or password incorrect");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                context.addCallbackParam("loggedIn", loggedIn);
            }
        } catch (Exception e) {
            //   JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/i18n/Bundle").getString("PersistenceErrorOccured"));
        }

    }

    public String checkManagerLogin() {
        try {
            List<TblManager> list = getTblManagerFacade().checkLogin(username, password);
            username = "";
            password = "";
            if (list.size() > 0) {
                managerLogin = list.get(0);
                JsfUtil.putSession(MANAGER_SESSION_KEY, managerLogin);
                return "app";
            }
            // JsfUtil.addErrorMessage(ResourceBundle.getBundle("/i18n/Bundle").getString("LoginFail"));
            return null;
        } catch (Exception e) {
            ///  JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/i18n/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }

    }

    public void logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }     
    }

    public void logoutManager() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(req.getContextPath() + "/faces/Admin/AdminLogin.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Creates a new instance of LoginController
     */
    public LoginController() {
    }
}
